$(function () {
    var table=document.getElementById("tbody-result");
    $.ajax({
        type:'GET',
        dataType:'json',
        url:contextPath+"/course/depend/"+theRequest.name,
        success:function (data) {
            console.log(data);
            var str="";
            for (var i=0;i<data.count;i++) {
                str+="<tr>" +
                    "<td>" + i + "</td>" +
                    "<td>" + data.list[i].lastcourse_name + "</td>" +
                    "<td>" + data.list[i].nextcourse_name + "</td>" +
                    "<td><button onclick='update(this)'>修改</button><button onclick='save(this)'>保存</button></td>"
                "</tr>"
            }
            table.innerHTML=str;
        },
        error:function () {
            alert("查询失败")
        }
    })
});
function update(r) {
    var table=document.getElementById("tbody-result");
    var i=r.parentNode.parentNode.rowIndex-1;
    table.rows[i].cells[1].innerHTML="<input type='text' id='lname' value=" + table.rows[i].cells[1].innerHTML+ " />";
    table.rows[i].cells[2].innerHTML="<input type='text' id='bname' value=" + table.rows[i].cells[2].innerHTML+ " />";
}
function save(r) {
    var table=document.getElementById("tbody-result");
    var i=r.parentNode.parentNode.rowIndex-1;
    var ss=new Object();
    ss.lastcourse_name=document.getElementById("lname").value;
    ss.nextcourse_name=document.getElementById("bname").value;
    $.ajax({
        type:'POST',
        url:contextPath+"update/depend",
        async: false,
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(ss),
        dataType:"json",
        success:function (data) {
            alert(data.message)
            window.location.reload();
        },
        error:function () {
            alert("修改失败");
        }
    })
}